package com.zfl.practice;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public class BankAccount {
    private String name;
    private List<Integer> balance = new ArrayList<>();
    private volatile int number;
    private final Lock lock = new ReentrantLock(true);
    private AtomicIntegerFieldUpdater<BankAccount> up = AtomicIntegerFieldUpdater
            .newUpdater(BankAccount.class,"number");


    public void addNum(){
        this.number++;
    }

    public void addByAtomic() {
        this.up.getAndIncrement(this);
    }
    public void add(int money) {
        lock.lock();
        try {

            this.balance.add(money);
        } finally {
            lock.unlock();
        }
    }

}
